<template>
    <div class="content-wrapper">
      <!-- Content Header (Page header) -->
      <section class="content-header">
        <h1>
          销售意向管理
        </h1>

        <ol class="breadcrumb">
          <li><a href="#"> 控制台</a></li>
          <li><a href="#">销售意向管理</a></li>
          <li class="active" v-if="$route.params.id == 0">新增</li>
          <li class="active" v-if="$route.params.id > 0">编辑</li>
        </ol>
      </section>

      <!-- Main content -->
      <section class="content">
          <div class="row">
            <div class="col-xs-12">
                <div class="box box-solid">
                  <div class="box-header with-border">
                    <h3 v-if="$route.params.id == 0" class="box-title">新增销售意向</h3>
                    <h3 v-if="$route.params.id > 0" class="box-title">编辑销售意向</h3>
                  </div>
                  <!-- /.box-header -->
                  <!-- form start -->
                  <form class="form-horizontal">
                    <div class="box-body">
                        <div class="row">
                            <div :class="['form-group', 'col-sm-6',{'has-error':errors.has('custom_name')}]">
                              <label class="col-sm-2 control-label">客户名称</label>
                              <div class="col-sm-10">
                                <input v-validate="'required|max:30'" name="custom_name" class="form-control" placeholder="30个字" v-model="model.custom_name">
                                <span class="help-block" v-if="errors.has('custom_name')">{{errors.first('custom_name')}}</span>
                              </div>
                            </div>
                            <div :class="['form-group', 'col-sm-6',{'has-error':errors.has('contact_name')}]">
                              <label  class="col-sm-2 control-label">联系人</label>
                              <div class="col-sm-10">
                                <input class="form-control" v-validate="'required|max:15'" name="contact_name" placeholder="15个字以内" v-model="model.contact_name">
                                <span class="help-block" v-if="errors.has('contact_name')">{{errors.first('contact_name')}}</span>
                              </div>
                            </div>
                        </div>
                        <div class="row">
                            <div :class="['form-group', 'col-sm-6',{'has-error':errors.has('contact_mobile')}]">
                              <label  class="col-sm-2 control-label">联系电话</label>
                              <div class="col-sm-10">
                                <input class="form-control" v-validate="'required|numeric|max:11'" name="contact_mobile" placeholder="11位有效手机号码" v-model="model.contact_mobile">
                                <span class="help-block" v-if="errors.has('contact_mobile')">{{errors.first('contact_mobile')}}</span>
                              </div>
                            </div>
                            <div :class="['form-group', 'col-sm-6',{'has-error':errors.has('origin')}]">
                              <label class="col-sm-2 control-label">机会来源</label>
                              <div class="col-sm-10">
                                <input class="form-control" v-validate="'max:20'" name="origin" placeholder="比如：某次聚餐会上" v-model="model.origin">
                                <span class="help-block" v-if="errors.has('origin')">{{errors.first('origin')}}</span>
                              </div>
                            </div>
                        </div>

                        <div class="row">
                            <div :class="['form-group', 'col-sm-6',{'has-error':errors.has('intention')}]">
                              <label class="col-sm-2 control-label">合作意向</label>
                              <div class="col-sm-10">
                                <input class="form-control" v-validate="'required|max:150'" name="intention" placeholder="150字内，比如：想购买我们公司的比本电脑" v-model="model.intention">
                                <span class="help-block" v-if="errors.has('intention')">{{errors.first('intention')}}</span>
                              </div>
                            </div>
                        </div>

                        <div class="row">
                            <div class="form-group col-sm-6">
                              <label  class="col-sm-2 control-label">创建人</label>
                              <div class="col-sm-10">
                                <input v-if="$route.params.id > 0" class="form-control" :value="model.user.name" readonly>
                                <input v-if="$route.params.id == 0" class="form-control" :value="user.name" readonly>
                              </div>
                            </div>

                            <div class="form-group col-sm-6">
                                <label  class="col-sm-2 control-label">创建时间</label>
                                <div class="col-sm-10">
                                  <input class="form-control" readonly v-model="model.created_at">
                                </div>
                           </div>
                        </div>

                    </div>
                    <!-- /.box-body -->
                    <div class="box-footer">
                      <button type="button" class="btn btn-primary" @click="submit">保存</button>
                    </div>
                    <!-- /.box-footer -->
                  </form>
                </div>
              <!-- /.box -->
            </div>
          </div>
      </section>
      <!-- /.content -->
    </div>
    <!-- /.content-wrapper -->
</template>

<script>

import SaleIntention from '@/modules/SaleIntention'
import {mapState} from 'vuex'
export default {

    mounted() {
        this.loadModel();
    },

    data() {
        return {
            model:{}
        };
    },

    methods: {



        loadModel() {
            if(this.$route.params.id > 0){
                SaleIntention.show(this.$route.params.id)
                .then((response) => {
                    this.model = response.contents;
                });
            }
        },

        submit() {
            this.$validator.validateAll().then((result) => {
                if(!result){
                    return;
                }

                var id = this.$route.params.id;

                if(id == 0){
                    SaleIntention.store(this.model)
                    .then((response) => {
                        $.message('操作成功');
                        this.$router.push({name:'SaleIntentionIndex'});
                    });
                }else{
                    SaleIntention.put(id,this.model)
                    .then((response) => {
                        $.message('操作成功');
                        this.$router.push({name:'SaleIntentionIndex'});
                    });
                }
            });
        }
    },

    computed:{
        ...mapState(['user'])
    }
}
</script>
